package com.runa.rccprotocol.analysis.zhengding;

import com.runa.monitor.comm.dto.newComm.HotMeterVO;
import com.runa.monitor.comm.dto.newComm.ValveVO;
import com.runa.monitor.comm.entity.CommMeter;
import com.runa.persistence.RepositoryServicePersis;
import com.runa.protocol.util.ByteUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 2024/8/28 11:08
 * zhangYB
 */
@Slf4j
public class ZhengDingModbusHandler {

    public Object handle(String netEquNo, int slaveId, int functionCode, byte[] data) {
        CommMeter commMeter = RepositoryServicePersis.getByNetSerialAndSequence(netEquNo, slaveId);
        if (null != commMeter) {
            return handleMeterData(netEquNo, commMeter.getSerial(), data);
        }
        return null;
    }

    public Object handleMeterData(String netEquNo, String equNo, byte[] data) {
        HotMeterVO meter = new HotMeterVO();
        meter.setMeterSerial(equNo);

        log.info("heatMeterData: {}", ByteUtil.byteToHexString(data));


        return meter;
    }


    public Object houseValveData(String netEquNo, String equNo, byte[] data, int index) {
        ValveVO valveVO = new ValveVO();
        valveVO.setValveSerial(equNo);

        int begin = index;

        byte addressCode = data[index++];
        byte functionCode = data[index++];
        byte byteCount = data[index++];

        log.info("houseValveData: {}", ByteUtil.byteToHexString(data, begin, byteCount+3));
        return valveVO;
    }
}
